//
//  MoveListViewController.m
//  UI_豆瓣项目
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 关业达. All rights reserved.
//

#import "MoveListViewController.h"
#import "MovieListTableViewCell.h"
#import "MovieListSameViewController.h"
#import "MBProgressHUD.h"
#import "AppTools.h"
#import "MovieDataViewController.h"
#import "MovieListSingleton.h"
@interface MoveListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *myTableView;

@property (nonatomic, retain)NSMutableArray *movieArray;

@property (nonatomic, retain)MBProgressHUD *hub;


@end

@implementation MoveListViewController

- (void)dealloc
{
    [_hub release];
    [_movieArray release];
    [_myTableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0.680 green:1.000 blue:0.529 alpha:1.000];
    self.navigationItem.title = @"电影";
    
    
    // 初始化数组
    self.movieArray = [NSMutableArray array];
    
    
    
    // 打开数据库
    [[MovieListSingleton shareMovieList] openMB];
    
    // 查询
    NSMutableArray *array = [NSMutableArray array];
    array = [[MovieListSingleton shareMovieList] selectMovie];
//    NSLog(@"array ===== %@", array);
    if (array != nil) {
        self.movieArray = [NSMutableArray arrayWithArray:array];
//        NSLog(@"movieArray ==== %@",self.movieArray);
        [self.myTableView reloadData];
    }
    
    
    
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    
    // 注册cell
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    
    
    
    
    // 请求数据
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";
    
    //菊花
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"别TM着急!!!";
    
    [AppTools geturlStr:urlStr Block:^(id result) {
        
        NSMutableDictionary *dic = result;
        NSString *error_code = [dic objectForKey:@"error_code"];
        NSString *reason = [dic objectForKey:@"reason"];
        NSMutableArray *array = [dic objectForKey:@"result"];
        if ([reason isEqualToString:@"success"]) {
            
            
            // 删除表
            [[MovieListSingleton shareMovieList] dropTable];
            
            // 创建表
            [[MovieListSingleton shareMovieList] createTable];
            
            for (NSDictionary *dic in array) {
                Movie *movie = [[Movie alloc] init];
                [movie setValuesForKeysWithDictionary:dic];
                [self.movieArray addObject:movie];
                // 插入表
                [[MovieListSingleton shareMovieList] insertMovie:movie];
              
                [movie release];
                
                // 刷新
                [self.myTableView reloadData];
                //停菊花
                [self.hub hide:YES];
            }
        }else{
            NSLog(@"请求失败");
        }
    }];
    
    
    
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_collection@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)] autorelease];
 
}

- (void)rightAction:(UIBarButtonItem *)barbtn
{
    MovieListSameViewController *moveSameVC = [[MovieListSameViewController alloc] init];
    [self.navigationController pushViewController:moveSameVC animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.movieArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    MovieListTableViewCell *cell = [[MovieListTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    Movie *movie = [self.movieArray objectAtIndex:indexPath.row];
    cell.aMovie = movie;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 180;
}

//点击触发
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MovieDataViewController *detailVC = [[MovieDataViewController alloc] init];
    Movie *movie = [self.movieArray objectAtIndex:indexPath.row];
    detailVC.movieID = movie.movieId;
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
